package com.yupi.usercenter.common;

import com.yupi.usercenter.exception.UserCenterExceptionEnum;
import lombok.Data;

/**
 * 统一结果返回类
 *
 * @param <T>
 * @author Administrator
 */
@Data
public class ApiRestResponse<T> {

    private Integer code;

    private String msg;

    private T data;

    public static final int OK_CODE = 10000;

    public static final String OK_NSG = "SUCCESS";

    public ApiRestResponse() {
        this(OK_CODE, OK_NSG);
    }

    public ApiRestResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiRestResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> ApiRestResponse<T> success(){
        return new ApiRestResponse<>();
    }

    public static <T> ApiRestResponse<T> success(T result) {
        ApiRestResponse<T> response = new ApiRestResponse<>();
        response.setData(result);
        return response;
    }

    public static <T> ApiRestResponse<T> error(Integer code, String msg) {
        return new ApiRestResponse<>(code, msg);
    }

    public static <T> ApiRestResponse<T> error(UserCenterExceptionEnum ex) {
        return new ApiRestResponse<>(ex.getCode(), ex.getMsg());
    }

    @Override
    public String toString() {
        return "ApiRestResponse{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
